var CAIZHONG_ARR = {
		"xg6hc": "0",
		"2fssc": "1",
		"xjssc": "1",
		"cqssc": "1",
		"tjssc": "1",
		"2fpk10": "2",
		"xysm": "2",
		"bjpk10": "2",
		"xyft": "2",
		"pl3": "3",
		"fc3d": "3",
		"shssl": "3",
		"ah11x5": "4",
		"jx11x5": "4",
		"sh11x5": "4",
		"sd11x5": "4",
		"gd11x5": "4",
		"bjk3": "5",
		"jsk3": "5",
		"gxk3": "5",
		"ahk3": "5",
		"jlk3": "5",
		"3fk3": "5",
		"gdkl10f": "6",
		"cqkl10f": "6",	
}
var CAIZHONG_OBJ = {
		"0":  {weishu: 7, max: "49", min: "1", len: "2",repeat:"1"},  //6hc
		"1":  {weishu: 5, max: "9", min: "0", len: "1",repeat:"3"},  //ssc
		"2":  {weishu: 10, max: "10", min: "1", len: "2",repeat:"1"},  //pk10
		"3":  {weishu: 3, max: "9", min: "0", len: "1",repeat:"0"},    // dpc, pcdd
		"4":  {weishu: 5, max: "11", min: "1", len: "2",repeat:"1"},   //11x5
		"5":  {weishu: 3, max: "6", min: "1", len: "1",repeat:"3"},    //kuai3
		"6":  {weishu: 8, max: "20", min: "1", len: "2",repeat:"1"},  //kl10f
}
var numZhengZe = /^[0-9+/,]+$/
var cname
var lotterType;


//通过彩种拿到彩种类型
function bindLotterType(cname){
	if(cname.indexOf("pk10")!=-1 || cname.indexOf("ft")!=-1 || cname=="xysm"){
		lotterType="2";
	}else if(cname.indexOf("6hc")!=-1){
		lotterType="0";
	}else if(cname.indexOf("ssc")!=-1){
		lotterType="1";
	}else if(cname.indexOf("28")!=-1 || cname=="shssl" || cname=="pl3" || cname=="fc3d"){
		lotterType="3";
	}else if(cname.indexOf("11x5")!=-1){
		lotterType="4";
	}else if(cname.indexOf("k3")!=-1){
		lotterType="5";
	}else if(cname.indexOf("kl10f")!=-1){
		lotterType="6";
	}
}

function createEmptyLotter(cname, node) {
	var html = "<input maxlength='80' class='form-control input-c-lotterNumber' required />"
	$(node).html("")
	$(node).append(html)
	if (cname === "xg6hc" && $(node).find(".jiahao-wrapper").length !== 0) {
		return
	}
	else if (cname !== "xg6hc") {
		$(node).find(".jiahao-wrapper").remove()
	}
	else if (cname === "xg6hc" && $(node).find(".jiahao-wrapper").length === 0) {
		var $allInputs = $(node).find(".input-c-lotterNumber")
		$allInputs.eq($allInputs.length - 1).before('<span class="jiahao-wrapper" style="width:8px;height:50px;margin-right:12px;display:inline-flex;justify-content:center;align-items:center;font-size:30px;">+</span>')
	}
}	
function mixLooterNumber(node) {
	var result = ""
	$(node).each(function(index, element) {
		result += element.value + ","
	})
	return result
}

//input实时监听输入的旗号并给出错误的提示框
function lotterInputListener(wapper,tips) {
	$(wapper).on("input", function(e) {
    	var flag = true
		var specialValue = $(e.currentTarget).val()
		if (specialValue == "") {
			$(tips).removeClass("active")
			return
		}
    	var count = 0  //记住重复出现的号码
    	var lotterData=CAIZHONG_OBJ[lotterType] //彩种类型 
    	var str = specialValue.split(",");
    	
    	if (!numZhengZe.test(specialValue)) {  //不是数字
    		$(tips).addClass("active").text("无效的开奖号码,开奖号码由数字和逗号组成")
    		flag = false
    		return;
    	}
    	for(i=0;i<str.length;i++){
    		var num=str[i]
    		if(str.length > lotterData.weishu){   
				$(tips).addClass("active").text("当前彩种开奖号码为"+lotterData.weishu+"位数")
				flag = false
			}
    		if(num !== ""){
				if(String(str[i]).length != lotterData.len){
					$(tips).addClass("active").text("当前彩种开奖号码格式为"+lotterData.len+"位数，如 "+lotterData.max)
					flag = false
				}
				
				if(num>Number(lotterData.max) || num<Number(lotterData.min)){
	    			$(tips).addClass("active").text("无效的开奖号码,开奖号码在"+lotterData.min+"~"+lotterData.max+"之间")
	    			flag = false
	    		}
			}
    		for(var j = i+1;j < str.length;j++){
    			if(lotterData.repeat==0){
    				return;
    			}else if(str[i] == str[j]){
    				count++;
    				if(lotterData.repeat==1){
    					$(tips).addClass("active").text("不能出现相同的号码")
    					flag = false
    				}else if(lotterData.repeat==3){
    					if(count > 3){
    						$(tips).addClass("active").text("不能出现3个相同的号码")
        					flag = false
    					}
    				}
					
				}	
			}
    	}
    	if (flag) {
    		$(tips).removeClass("active")
    	} else {
    		$(tips).addClass("active")
    	}
	})	
}

//开奖号码不能重复
function isLotterRepeat() {
	var flag = true
	var allNumberInputs = document.querySelectorAll("#p-i-lotterNumWrapper .input-c-lotterNumber")
	outFor:
	for (var i = 0; i < allNumberInputs.length; i++) {
		var item = allNumberInputs[i]
 		for (var j = 0; j < allNumberInputs.length; j++) {
			var item2 = allNumberInputs[j]
			if (j == i) {continue}
			if (item2.value == item.value) {
				flag = false
				break outFor
			}
		} 
	}
	if (!flag) {
		return "开奖号码不得出现重复"
	}
	return "ok"	
}

